package cn.echo.modules.sys.service;



import cn.echo.common.baseservice.BaseService;
import cn.echo.modules.sys.dao.ArticleMapper;
import cn.echo.modules.sys.dao.CategoryMapper;
import cn.echo.modules.sys.entity.Article;
import cn.echo.modules.sys.entity.Category;
import cn.echo.modules.sys.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryService extends BaseService {

            @Autowired
            private CategoryMapper categoryMapper;
      public List<Category> findAll(Category category){
          List<Category>categoryList=categoryMapper.findAll(new Category());
//          SqlSession session = getSession(true);
//          List<Category> categoryList=session.getMapper(CategoryMapper.class).findAll(new Category());
//          session.close();
          return categoryList;
      }
      public boolean deleteById(Category category){

//          SqlSession session=getSession(true);
//          boolean  isSuccess=session.getMapper(CategoryMapper.class).deleteById(category);
//          return isSuccess;
          boolean  isSuccess = categoryMapper.deleteById(category);
          return isSuccess;
      }
      public boolean insert(Category category){
//          SqlSession session=getSession(true);
//      CategoryMapper categoryMapper=session.getMapper(CategoryMapper.class);
          boolean add=false;
          try {
              category.setCreateDate(new Date());
              add = categoryMapper.insert(category);
          }catch(Exception e){
              e.printStackTrace();
          }
          return add;
      }
    public Category findById(Category category) {
//        SqlSession session = null;
//        session = MyBatisUtils.getSqlFactory().openSession();
//        CategoryMapper categoryMapper = session.getMapper(CategoryMapper.class);
//        try {
//            return categoryMapper.findByIdReturnByResultMap(category);
//        } finally {
//            session.close();
//        }
        return categoryMapper.findByIdReturnByResultMap(category);
    }
    public boolean update(Category category) {
//        SqlSession session =getSession(true);
//        CategoryMapper categoryMapper = session.getMapper(CategoryMapper.class);
        boolean flag = false;
        try {
            category.setUpdateDate(new Date());
            flag = categoryMapper.update(category);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        finally {
//            session.close();
//        }
        return flag;
    }
}
